// pages/teacher/banji/index.js
import {
  getBanji,
  joinBanji
} from "../../../api/student";
import {
  parseTime
} from "../../../utils/datetime.js";
const app = getApp()

Component({

  /**
   * 组件的属性列表
   */
  properties: {

  },

  /**
   * 组件的初始数据
   */
  data: {
    arr: [],
    refreshing: false,
    pageNo: 1,
    pageSize: 10,
    loadMore: true,
    classid: '',
  },
  lifetimes: {
    attached: function () {
      this.loadData()
    }
  },

  /**
   * 组件的方法列表
   */

  methods: {
    loadData: function () {
      let data = {
        pageNo: this.data.pageNo,
        pageSize: this.data.pageSize
      }

      getBanji(data).then(res => {
        res.data.forEach(item => {
          item.time = parseTime(item.time, "{y}/{m}/{d}")
        })
        let arr = this.data.arr
        arr = arr.concat(res.data)
        this.setData({
          arr: arr,
          refreshing: false,
          loadMore: res.data.length == this.data.pageSize
        })
      })

    },
    //携带数据跳转签到页面
    qiandao: function (e) {
      let index = e.currentTarget.dataset.index
      let data = this.data.arr[index]
      // let classname = this.data.arr[index]["classname"]
      // let classid = this.data.arr[index]["id"]
      // let kq_state = this.data.arr[index]["kq_state"]
      // let data={
      //   classid:classid,
      //   classname:classname,
      //   kq_state:kq_state
      // }
      wx.navigateTo({
        url: '/pages/student/qiandao/index',
        success: res => {
          res.eventChannel.emit("send", data)
        },
      })
    },
    onScan: function () {
      wx.scanCode({
        onlyFromCamera: true,
        scanType: ['qrCode'],
        success: res => {
          let classid = res.result
          this.setData({
            classid: classid
          })
          console.log(this.data.classid);
          let data = {
            classid: this.data.classid
          }
          joinBanji(data).then(res => {
            // console.log("okkk", res)
            let {
              code,
              msg,
            } = res
            if (code === 500) {
              wx.showToast({
                title: msg,
                icon: 'none'
              })
            } else {
              wx.showToast({
                title: "班级加入成功",
              })
              this.onRefresh()
            }
          })
        },
        fail: res => {
          console.log("扫码取消")
        }
      })
    },


    data: {
      show: false,
    },

    onClickShow() {
      this.setData({
        show: true
      });
    },

    onClickHide() {
      this.setData({
        show: false
      });
    },

    noop() {},

    //下拉刷新
    onRefresh: function () {
      this.setData({
        pageNo: 1,
        arr: []
      })
      this.loadData()
    },

    //上拉加载（分页）
    onMore: function () {
      if (this.data.loadMore === false) return
      let pageNo = this.data.pageNo
      pageNo++
      this.setData({
        pageNo
      })
      this.loadData()
    },
  },
  lifetimes: {
    ready() {
      this.setData({
        pageNo: 1,
        arr: []
      })
      this.loadData()
    }
  }
})